﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MaxZhang.EasyEntities.EasyDesigner.Core;
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;



namespace MaxZhang.EasyEntities.EasyDesigner.UserControls
{
    /// <summary>
    /// Interaction logic for TableSchemaList.xaml
    /// </summary>
    public partial class TableSchemaList : UserControl
    {
        public TableSchemaList()
        {
            InitializeComponent();
        }

        public TableSchemaList(DbEntity entity):this()
        {
            this.DataContext = entity;
        }

        public MainWindow MW { set; get; }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            VelocityEngine velocity = new VelocityEngine();

            ExtendedProperties props = new ExtendedProperties();
            velocity.Init(props);
            Template template = velocity.GetTemplate(@"codeTemplate.vm");
            VelocityContext context = new VelocityContext();
            context.Put("entity", this.DataContext as DbEntity);
        

            StringWriter writer = new StringWriter();
            template.Merge(context, writer);
            MW.editer.Document.Text = writer.GetStringBuilder().ToString();


        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            label1.MouseDoubleClick += new MouseButtonEventHandler(label1_MouseDoubleClick);
        }

        void label1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            tb1.Visibility = Visibility.Visible;
            label1.Visibility = Visibility.Collapsed;
            tb1.LostFocus +=(s,ev)=>{
                    tb1.Visibility = Visibility.Collapsed;
                    label1.Visibility =Visibility.Visible ;
                    
            };
        }

      

    }
}
